<?PHP

$password = $_POST['password'];
$repeat_password = $_POST['repeat_password'];
$name = $_POST['name'];
$sex = $_POST['sex'];
$volunteer_position = $_POST['volunteer_position'];
$work_days = $_POST['work_days'];
$phone_number = $_POST['phone_number'];
$email = $_POST['email'];
$clothing_model = $_POST['clothing_model'];
$secret_key = $_POST['secret_key'];
if (!empty($_POST['bank_account'])) {
    $bank_account = $_POST['bank_account'];
} else {
    $bank_account = '';
}
if (!empty($_GET['flag'])) {
    $flag = $_GET['flag'];
} else {
    $flag = '';
}

$mysqli = mysqli_init();

$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 2); //设置超时时间
$mysqli->real_connect('127.0.0.1', 'root', 'mysql');
$sql = "CREATE DATABASE IF NOT EXISTS CUPT DEFAULT CHARSET utf8 COLLATE utf8_general_ci;";
if ($mysqli->query($sql) == TRUE) {
    //printf("Table CUPT successfully created.\n");
}
mysqli_select_db($mysqli, "CUPT");
/*//建表
$sql = "CREATE TABLE IF NOT EXISTS `volunteer` (
        `password` varchar(30) DEFAULT NULL,
        `name` varchar(20) DEFAULT NULL,
        `sex` varchar(4) DEFAULT NULL,
        `volunteer_position` varchar(20) DEFAULT NULL,
        `work_days` varchar(3) DEFAULT NULL,
        `phone_number` varchar(20) DEFAULT NULL,
        `email` varchar(20) DEFAULT NULL,
        `bank_account` varchar(30) DEFAULT NULL,
        `clothing_model` varchar(10) DEFAULT NULL,
        `secret_key` varchar(10) DEFAULT NULL,
        UNIQUE KEY(`email`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";

if (mysqli_query($mysqli, $sql)) {
    //echo "数据表 participant 创建成功";
} else {
    //echo "创建数据表错误: " . mysqli_error($mysqli);
}*/

if ($flag == 0) {
    $email_existed = '';
    $sql = "select * from volunteer where `email`='$email';";
    $rst = $mysqli->query($sql);
    $sql2 = "select email from volunteer where `secret_key`='$secret_key';";
    $rst2 = $mysqli->query($sql2);
    while ($row = mysqli_fetch_assoc($rst2)) {
        $email_existed = $row['email'];
    }

    if ($repeat_password !== $password) {
        echo "<br />两次输入密码不一致<br />";
        echo '<a href="javascript :;" onClick="javascript :history.back(-1);">回到注册界面</a>';
    } elseif ($rst->num_rows != 0) {
        echo "<br />该邮箱已被注册<br />";
        echo '<a href="javascript :;" onClick="javascript :history.back(-1);">回到注册界面</a>';
    } elseif ($rst2->num_rows == 0 || $email_existed != '') {
        echo "<br />管理员秘钥错误<br />";
        echo '<a href="javascript :;" onClick="javascript :history.back(-1);">回到注册界面</a>';
    } else {
//$sql = "describe participant;";
//插入数据

        $sql1 = "SET SQL_SAFE_UPDATES = 0;";
        $sql = "UPDATE volunteer SET email='$email',password='$password',name ='$name',sex='$sex',volunteer_position='$volunteer_position',work_days='$work_days',phone_number='$phone_number',bank_account='$bank_account',clothing_model='$clothing_model' WHERE `secret_key` = '$secret_key';";
        $sql2 = "SET SQL_SAFE_UPDATES = 0;";
//echo "<br />".$sql;
        $mysqli->query($sql1);
        $mysqli->query($sql);
        $mysqli->query($sql2);
//echo "<br />注册成功<br />";
        mysqli_close($mysqli);
        header("Location: admin-signin.html?email=$email&signup_flag=1");
        exit;
    }
} elseif ($flag == 1) {
    $sql = "select * from volunteer where `email`='$email' and `password`='$password';";
    $rst = $mysqli->query($sql);
    if ($repeat_password !== $password) {
        echo "<br />两次输入密码不一致<br />";
        echo '<a href="javascript :;" onClick="javascript :history.back(-1);">回到注册界面</a>';
    } elseif ($rst->num_rows == 0) {
        echo "邮箱不存在或密码错误";
        echo '<a href="javascript :;" onClick="javascript :history.back(-1);">回到注册界面</a>';
    } else {
        $sql = "UPDATE volunteer SET name ='$name',sex='$sex',volunteer_position='$volunteer_position',work_days='$work_days',phone_number='$phone_number',bank_account='$bank_account',clothing_model='$clothing_model' WHERE `email` = '$email';";
        $mysqli->query($sql);
        echo "管理员信息修改成功";
    }
}

?>